from jsonpath import jsonpath
from loguru import logger


# 封装一个类断言响应结果
class HandleAssertResponse:

    def assert_response(self, response_dict, assert_response_expr_str):
        """
        断言响应结果
        :param response_dict: 字典类型。本框架当中主要是http请求的响应数据。
        :param assert_response_expr_str:
            断言表达式，字符串类型。是列表形式的字符串。列表当中的每个成员，是字典格式。
            [
                {"jsonpath_expression":"$..code","expected":0,"compare_type":"=="},
                {"jsonpath_expression":"$..msg","expected":"OK","compare_type":"=="},
                {"jsonpath_expression":"$..reg_name","expected":"科万代理商","compare_type":"=="}
            ]
        :return: True或者False。如果为True,表示所有的断言均成功。如果为False，表示有断言失败。
        """

        # 公开仓库去掉部分代码，已移交至私有仓库。


if __name__ == '__main__':
    handle_assert_response = HandleAssertResponse()
    response_dict = {
        "code": 0,
        "msg": "OK",
        "data": {
            "id": 1236907132,
            "reg_name": "科万代理商",
            "mobile_phone": "18688881115"
        },
        "copyright": "Copyright 科万代理商 All Rights Reserved"
    }

    assert_response_expr_str = """[{"jsonpath_expression":"$..code","expected":0,"compare_type":"=="},
    {"jsonpath_expression":"$..msg","expected":"OK","compare_type":"=="},
    {"jsonpath_expression":"$..reg_name","expected":"科万代理商","compare_type":"=="}]"""
    res = handle_assert_response.assert_response(response_dict, assert_response_expr_str)
    print(res)
